<?php

namespace app\common\model;

use function now_time;

/**
 * 分类
 *
 * @property-read mixed $explain
 * @property-read mixed $operation
 */
class AdminActionLog extends \app\common\model\BaseModel {

	protected $resultSetType = 'collection';


	protected $type = [
		'header' =>'json',
		'request_params' =>'json',
		'response_params'=>'json'
	];
	/**
	 * 表名,
	 */
	protected $table = 'admin_action_log';

	protected $modules = [
		'AdminActionLog'          => '后台操作日志',
		'AdminAuthGroup'          => '后台管理组',
		'AdminAuthMenu'           => '后台管理菜单',
		'AdminAuthMenuSelectList' => '后台管理菜单',
		'AdminUser'               => '后台管理员',
		'AdminUserResetPassword'  => '重置密码',
		'Article'                 => '文章',
		'ArticleCate'             => '文章分类',
		'ArticleForType'          => '文章类别',
		'Banner'                  => '轮播图',
		'Company'                 => '分院',
		'Customer'                => '客户',
		'CustomerFollowPlain'     => '客服跟进',
		'Login'                   => '登录',
		'Message'                 => '消息',
		'Project'                 => '项目',
		'ProjectDecorationLog'    => '装修日志',
		'ProjectLog'              => '项目日志',
		'ProjectMatters'          => "项目事项",
		'ProjectNode'             => '项目节点',
		'Report'                  => '报表',
		'Staff'                   => '员工',
		'SysSetting'              => '系统设置',
		'Upload'                  => '文件上传',
		'User'                    => '用户',
		'UserFeedback'            => '留言',
		'Warning'                 => '警告',
		'ProjectData'             => '项目资料',
		'AdminAuthMenuTree'       => '用户权限',
		'ProjectMaterial'       => '项目主材',
		'ProjectPlan'       => '周计划',
	];

	protected $methods = [
		'GET'    => '查询数据',
		'POST'   => '新增数据',
		'PUT'    => '修改数据',
		'DELETE' => '删除数据'
	];

	public function getExplainAttr($value, $data) {
		if ($value) return $value;
		if (isset($this->modules[$data['controller']])) {
			return $this->modules[$data['controller']] . '-' . $this->methods[$data['methods']];
		}
		return $data['controller'];
	}

	public function getOperationAttr($value, $data) {
		if ($value) return $value;
		if (isset($this->modules[$data['controller']])) {
			return $this->modules[$data['controller']];
		}
		return $data['controller'];
	}

	public static function build() {
		return new self();
	}

	public function char($request) {
		//

		if (!empty($request['start_time'])) {
			$startTime = $request['start_time'];
		} else {
			$startTime = now_time(946659661);
		}
		if (!empty($request['end_time'])) {
			$endTime = $request['end_time'];
		} else {

			$endTime = now_time(1893430861);
		}
		$list = $this->field('*')
		             ->where('menu_name|operation|obj|admin_name', 'like', "{$request['keyword_search']}%")
		             ->order('create_time desc')
		             ->whereTime('create_time', 'between', [$startTime, $endTime])
		             ->paginate(['list_rows' => $request['page_size'] ?? 25, 'page' => $request['page_index'] ?? 1]);
		return $list;

	}

	public function add($json, $user_info) {
		$data['admin_name'] = $user_info['name'] ?? $user_info['account'];
		$data['admin_uuid'] = $user_info['id'];
		if (!empty($json['menu_name'])) {
			$data['menu_name'] = $json['menu_name'];
		}
		if (!empty($json['operation'])) {
			$data['operation'] = $json['operation'];
		}

		if (isset($json['explain'])) {
			$data['explain'] = $json['explain'];
		}

		if (isset($json['admin_ip'])) {
			$data['admin_ip'] = $json['admin_ip'];
		}

		$data['create_time'] = now_time(time());
		return $this->insert($data);
	}
}